package com.guigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.constant.MessageConstant;
import com.guigu.dao.CheckItemMapper;
import com.guigu.entity.PageResult;
import com.guigu.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 检查项服务
 */

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemDao;
    /**
     * 新增检查项
     *
     * @param checkItem
     */
    @Override
    public boolean add(CheckItem checkItem) {
        int add = checkItemDao.add(checkItem);
        if(add>0){
            return true;
        }
        return false;
    }

    /**
     * 在CheckItemService服务接口中扩展分页查询方法
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page =checkItemDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id删除检查项
     *
     * @param id
     */
    @Override
    public boolean delete(Integer id) {
        long count=checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            // 被使用了
            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_GROUP_FAIL);
        }
        checkItemDao.deleteById(id);
        System.out.println(count);
        return true;
    }

    /**
     * 编辑检查项
     *
     * @param checkItem
     */
    @Override
    public boolean edit(CheckItem checkItem) {
        int edit = checkItemDao.edit(checkItem);
        if(edit>0){
            return true;
        }
        return false;
    }

    /**
     * 根据id查询checkItem
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }


    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
